package com.younion.business.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.younion.business.IMemberCardService;
import com.younion.common.paginator.Paginator;
import com.younion.dao.MemberCardDAO;
import com.younion.dao.RechargeFeedbackTaskLogDAO;
import com.younion.dao.RechargeLogDAO;
import com.younion.vo.API_MemberCardVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.RechargeFeedbackTaskLogVO;
import com.younion.vo.RechargeLogVO;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

 
public class MemberCardServiceImpl implements IMemberCardService {

	protected static final Log logger = LogFactory.getLog(MemberCardServiceImpl.class);

	private MemberCardDAO memberCardDAO;
	@Autowired
	private RechargeLogDAO rechargeLogDAO;
	@Autowired
	private RechargeFeedbackTaskLogDAO rechargeFeedbackTaskLogDAO;
	
	public Collection getAll() throws Exception {
		return memberCardDAO.selectAll();
	}

	public MemberCardVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id)<1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = memberCardDAO.selectById(paramMap);
			return obj==null?null:(MemberCardVO)obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public MemberCardVO get(String id) error occur:",e);
		}
	}
	public Collection getByMap(Map paramMap) throws BaseException {
		try{
			return memberCardDAO.selectByMap(paramMap);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public Collection getByMap(Map map) error occur:",e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return memberCardDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()
					+ " >> public Collection getCountByMap(Map map) error occur:", e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if (MapCollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return memberCardDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public Page (Map paramMap, Paginator paginator) error occur:",e);
		}
	}

	public MemberCardVO save(MemberCardVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
				memberCardDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (MemberCardVO)memberCardDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public MemberCardVO save(MemberCardVO voObject) error occur:",e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				MemberCardVO data = (MemberCardVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public Collection save(Collection voList) error occur:",e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try{
			memberCardDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean remove(List ids) error occur:",e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			memberCardDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean removeByMap(Map paramMap) error occur:",e);
		}
	}
	
	public boolean updateMemberCardMoney(List<Map<String,Object>> updateMaps, List<RechargeLogVO> rechangeLogs) throws Exception {
		if (CollectionUtils.isEmpty(updateMaps))
			return false;
		try{
			rechargeLogDAO.batchInsert(rechangeLogs);
			for(Map<String,Object>  map : updateMaps ){
				memberCardDAO.recharge(map);
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean recharge(Map map) error occur:",e);
		}
	}
	
	public boolean updateCouponAmounts(Map map) throws Exception {
		if (CollectionUtils.isEmpty(map))
			return false;
		try{
			memberCardDAO.updateCouponAmounts(map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean updateCouponAmounts(Map map) error occur:",e);
		}
	}
	
	public List getMyCardByUserToken(String userToken) throws Exception{
		try {
			return memberCardDAO.getMyCardByUserToken(userToken);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					MemberCardServiceImpl.class.getName()
							+ " >> public List getMyCardByUserToken(String userToken) error occur:",
					e);
		}
	}
	
	public boolean updateCouponAmount(Map map) throws Exception {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			memberCardDAO.updateCouponAmount(map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean updateCouponAmountById(String id) error occur:",e);
		}
	}
	
	public Collection getByUserToken(String userToken) throws BaseException {
		if(StringUtils.isEmpty(userToken)){
			return null;
		}
		try{
			return memberCardDAO.selectByUserToken(userToken);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public Collection getByUserToken(String userToken) error occur:",e);
		}
	}
	
	public List selectByIds(List ids) throws Exception {
		return memberCardDAO.selectByIds(ids);
	}
	
	public List<MemberCardVO> selectByFeedbackCount(Map paramMap) throws BaseException{
		try {
			return memberCardDAO.selectByFeedbackCount(paramMap);
		} catch (com.younion.common.exception.BaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public int saveReturnKey(MemberCardVO memberCardVO) throws Exception{
		if (memberCardVO==null)
			return 0;
		try {
			return memberCardDAO.saveReturnKey(memberCardVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public int saveReturnKey(MemberCardVO memberCardVO)  error occur:",e);
		}
	}
	public API_MemberCardVO getApiMemberCardVOByMap(Map paramMap) throws Exception{
		try {
			Object obj = memberCardDAO.selectApiMemberCardVOByMap(paramMap);
			return obj==null?null:(API_MemberCardVO)obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public MemberCardVO getMemberCardVOByMap(Map paramMap)  error occur:",e);
		}
	}
	
	public boolean updateRechargeFeekback(List<Map<String,Object>> updateMaps, List<RechargeFeedbackTaskLogVO> rechangeTaskLogs) throws Exception {
		if (CollectionUtils.isEmpty(updateMaps))
			return false;
		try{
			for(RechargeFeedbackTaskLogVO  log : rechangeTaskLogs ){
				rechargeFeedbackTaskLogDAO.insertByVo(log);
			}
			for(Map<String,Object>  map : updateMaps ){
				memberCardDAO.updateByMap(map);
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean recharge(Map map) error occur:",e);
		}
	}
	
	public boolean updateToMobileUser(Integer userId) throws Exception{
		try {
			return memberCardDAO.updateToMobileUser(userId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(MemberCardServiceImpl.class.getName()+" >> public boolean updateToMobileUser(Integer userId) error occur:",e);
		}
	}
	/**
	 * @param service
	 */
	public void setMemberCardDAO(MemberCardDAO memberCardDAO) {
		this.memberCardDAO = memberCardDAO;
	}

	public RechargeLogDAO getRechargeLogDAO() {
		return rechargeLogDAO;
	}

	public void setRechargeLogDAO(RechargeLogDAO rechargeLogDAO) {
		this.rechargeLogDAO = rechargeLogDAO;
	}

	public MemberCardDAO getMemberCardDAO() {
		return memberCardDAO;
	}

	public RechargeFeedbackTaskLogDAO getRechargeFeedbackTaskLogDAO() {
		return rechargeFeedbackTaskLogDAO;
	}

	public void setRechargeFeedbackTaskLogDAO(
			RechargeFeedbackTaskLogDAO rechargeFeedbackTaskLogDAO) {
		this.rechargeFeedbackTaskLogDAO = rechargeFeedbackTaskLogDAO;
	}
	
}